import javax.swing.JOptionPane;



/**
 * GradeStatistics.java
 *
 * A skeleton for the class to be filled in by you
 *
 *
 * @author Clint
 * @Modified by: Andrew Shore
 * @version 1.0
 */

public class GradeStatistics {
   
   
    /*
     * Postcondition: returns the minimum grade in this.
     */
    public static int findMin(int[] theArray) {
	int min = 1000;
	for(int index=0; index<theArray.length; index++){
		if(theArray[index]<min){
			min=theArray[index];
		}
	}
	return min;
    }
    
     /*
     * Postcondition: returns the maximum grade in this.
     */
    public static int findMax(int[] theArray) {
	int max = 0;
	for(int index=0; index<theArray.length; index++){
		if(theArray[index]>max){
			max=theArray[index];
			
		}
	}
	return max;
    }
      
     /*
     * Postcondition: returns the mean or average grade of theArray.
     */
    public static double computeMean(int[] theArray) {
	double mean = 0.0;
	double total = 0;
	
	for(int index=0; index<theArray.length; index++){
		total += theArray[index];
	}
	mean=total/theArray.length;


	return mean;
    }
   
    
    /**
     * Postcondition: returns the standard deviation of the
     * grades in theArray.
     */ 
    public static double computeStandardDeviation(double mean, int[] theArray) {
	double standardDeviation = 0.0;
	double sum=0;
	//compute the standard deviation
	for(int counter=0; counter<theArray.length;counter++){
		sum += (theArray[counter]-GradeStatistics.computeMean(theArray))*(theArray[counter]-GradeStatistics.computeMean(theArray));
		double deviation=(sum)/theArray.length;
		standardDeviation=Math.sqrt(deviation);
	}


	return standardDeviation;//just to make the compiler happy
    }

    /**
     * The main method, this is where you implement your main
     * algorithm
     */
    public static void main(String[] args){
       JOptionPane.showMessageDialog(null, "Welcome to the Grade Statistics program.");
       boolean isContinue =true;
       
       while(isContinue){
    	   String grades=JOptionPane.showInputDialog(null, "Please enter how many students took the exam:");
    	   int numberOfGrades=Integer.parseInt(grades);
    	   int [] gradeStatistics=new int[numberOfGrades];
    	   
    	   for(int counter=0; counter<numberOfGrades; counter++){
    		  String grade= JOptionPane.showInputDialog(null, "Please enter the test grade for student "+(counter+1));
    		  int intGrade=Integer.parseInt(grade);
    		   gradeStatistics[counter]=intGrade;
    		   
    	   }
    	   JOptionPane.showMessageDialog(null, "The minimum test score is: "+GradeStatistics.findMin(gradeStatistics));
    	   JOptionPane.showMessageDialog(null, "The maximum test score is: "+GradeStatistics.findMax(gradeStatistics));
    	   JOptionPane.showMessageDialog(null, "The mean score is: "+GradeStatistics.computeMean(gradeStatistics));
    	   JOptionPane.showMessageDialog(null, "The standard deviation is: "+GradeStatistics.computeStandardDeviation
    			   (GradeStatistics.computeMean(gradeStatistics), gradeStatistics));
    	  String answer= JOptionPane.showInputDialog(null, "Do you want to compute the statistics for another exam? (Yes/No)");
    	   if(answer.equalsIgnoreCase("yes")){
    		   
    		   
    	   }
    		   
    		  if(answer.equalsIgnoreCase("no")){
    		   isContinue=false;
    		   JOptionPane.showMessageDialog(null, "Thank you for using the Grade Statistics program. Goodbye.");
    		   
    	   }
       }

	
    }
}